import Cookies from "js-cookie";

/**
 * sessionStorage
 */
export const getCache = key => {
  if (!sessionStorage) {
    return null;
  }
  if (key == null) {
    return null;
  }
  return sessionStorage.getItem(key);
};
export const setCache = (key, value) => {
  if (!sessionStorage) {
    return;
  }
  if (key != null && value != null) {
    sessionStorage.setItem(key, value);
  }
};
export const getJsonCache = key => {
  const value = getCache(key);
  if (value != null) {
    return JSON.parse(value);
  }
};
export const setJsonCache = (key, value) => {
  if (value != null) {
    setCache(key, JSON.stringify(value));
  }
};
export const removeCache = key => {
  return sessionStorage.removeItem(key);
};

/**
 * localStorage
 */
export const getLocalCache = key => {
  if (!localStorage) {
    return null;
  }
  if (key == null) {
    return null;
  }
  return localStorage.getItem(key);
};
export const setLocalCache = (key, value) => {
  if (!localStorage) {
    return;
  }
  if (key != null && value != null) {
    localStorage.setItem(key, value);
  }
};
export const getLocalJsonCache = key => {
  const value = getLocalCache(key);
  if (value != null) {
    return JSON.parse(value);
  }
};
export const setLocalJsonCache = (key, value) => {
  if (value != null) {
    setLocalCache(key, JSON.stringify(value));
  }
};
export const removeLocalCache = key => {
  return localStorage.removeItem(key);
};

/**
 * Cookies
 * @return {*}
 */
export const getCookies = key => {
  return Cookies.get(key);
};
export const setCookies = (key, data) => {
  return Cookies.set(key, data);
};
export const removeCookies = key => {
  return Cookies.remove(key);
};
